package com.zp.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.base.api.entity.PriceListLog;
import com.zp.base.entity.bo.PriceListLogBo;
import com.zp.base.entity.vo.PriceListLogVo;
import com.zp.base.mapper.PriceListLogMapper;
import com.zp.base.service.IPriceListLogService;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 价表操作记录Service业务层处理
 *
 * @author zhang peng
 * @date 2023-01-07
 */
@RequiredArgsConstructor
@Service
public class PriceListLogServiceImpl implements IPriceListLogService {

    private final PriceListLogMapper baseMapper;

    /**
     * 查询价表操作记录
     */
    @Override
    public PriceListLogVo queryById (String priceLogId) {
        return baseMapper.selectVoById(priceLogId);
    }

    /**
     * 查询价表操作记录列表(分页)
     */
    @Override
    public TableDataInfo<PriceListLogVo> queryPageList (PriceListLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PriceListLog> lqw = buildQueryWrapper(bo);
        Page<PriceListLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询价表操作记录列表(不分页)
     */
    @Override
    public List<PriceListLogVo> queryList (PriceListLogBo bo) {
        LambdaQueryWrapper<PriceListLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PriceListLog> buildQueryWrapper (PriceListLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PriceListLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getItemClass()), PriceListLog::getItemClass, bo.getItemClass());
        lqw.eq(StringUtils.isNotBlank(bo.getItemCode()), PriceListLog::getItemCode, bo.getItemCode());
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), PriceListLog::getItemName, bo.getItemName());
        lqw.eq(StringUtils.isNotBlank(bo.getMedicalInsuranceType()), PriceListLog::getMedicalInsuranceType, bo.getMedicalInsuranceType());
        lqw.eq(StringUtils.isNotBlank(bo.getItemSpec()), PriceListLog::getItemSpec, bo.getItemSpec());
        lqw.eq(StringUtils.isNotBlank(bo.getUnits()), PriceListLog::getUnits, bo.getUnits());
        lqw.eq(bo.getNetworkPrice() != null, PriceListLog::getNetworkPrice, bo.getNetworkPrice());
        lqw.eq(bo.getTradePrice() != null, PriceListLog::getTradePrice, bo.getTradePrice());
        lqw.eq(bo.getPrice() != null, PriceListLog::getPrice, bo.getPrice());
        lqw.eq(bo.getPreferPrice() != null, PriceListLog::getPreferPrice, bo.getPreferPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getClassOnRcpt()), PriceListLog::getClassOnRcpt, bo.getClassOnRcpt());
        lqw.eq(StringUtils.isNotBlank(bo.getClassOnInp()), PriceListLog::getClassOnInp, bo.getClassOnInp());
        lqw.eq(StringUtils.isNotBlank(bo.getClassOnReckoning()), PriceListLog::getClassOnReckoning, bo.getClassOnReckoning());
        lqw.eq(StringUtils.isNotBlank(bo.getSubjCode()), PriceListLog::getSubjCode, bo.getSubjCode());
        lqw.eq(StringUtils.isNotBlank(bo.getClassOnMr()), PriceListLog::getClassOnMr, bo.getClassOnMr());
        lqw.eq(StringUtils.isNotBlank(bo.getIsNer()), PriceListLog::getIsNer, bo.getIsNer());
        lqw.eq(StringUtils.isNotBlank(bo.getPriceId()), PriceListLog::getPriceId, bo.getPriceId());
        lqw.eq(bo.getStartDate() != null, PriceListLog::getStartDate, bo.getStartDate());
        lqw.eq(bo.getStopDate() != null, PriceListLog::getStopDate, bo.getStopDate());
        lqw.eq(StringUtils.isNotBlank(bo.getInputCode()), PriceListLog::getInputCode, bo.getInputCode());
        lqw.eq(StringUtils.isNotBlank(bo.getOperationType()), PriceListLog::getOperationType, bo.getOperationType());
        lqw.eq(PriceListLog::getOrgId, bo.getOrgId());
        lqw.eq(StringUtils.isNotBlank(bo.getItemCodeYbCompare()), PriceListLog::getItemCodeYbCompare, bo.getItemCodeYbCompare());
        lqw.between(params.get("beginTime") != null && params.get("endTime") != null,
                PriceListLog::getCreateTime, params.get("beginTime"), params.get("endTime"));
        lqw.orderByDesc(PriceListLog::getCreateTime);
        return lqw;
    }

    /**
     * 新增价表操作记录
     */
    @Override
    public Boolean insertByBo (PriceListLogBo bo) {
        PriceListLog add = BeanUtil.toBean(bo, PriceListLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPriceLogId(add.getPriceLogId());
        }
        return flag;
    }

    /**
     * 修改价表操作记录
     */
    @Override
    public Boolean updateByBo (PriceListLogBo bo) {
        PriceListLog update = BeanUtil.toBean(bo, PriceListLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (PriceListLog entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除价表操作记录
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
